package com.xuecheng.media.interfaces.web;

import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.application.service.MediaFilesApplication;
import com.xuecheng.media.application.vo.req.FileUploadQry;
import com.xuecheng.media.application.vo.req.MediaFilesPageQry;
import com.xuecheng.media.application.vo.resp.FileUploadResultDto;
import com.xuecheng.media.application.vo.resp.MediaFilesDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaFilesApi {

    @Autowired
    private MediaFilesApplication mediaFilesApplication;

    @ApiOperation("上传课程文件")
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileUploadResultDto upload(@RequestPart("filedata") MultipartFile multipartFile,
                                      @RequestParam(value = "objectName",required = false) String objectName){
        Long companyId = 1232141425L; //todo

        return mediaFilesApplication.uploadFile(multipartFile, companyId, objectName);
    }

    @ApiOperation("大文件上传前检验")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile(@Validated FileUploadQry fileUploadQry){

        return mediaFilesApplication.checkFile(fileUploadQry.getFileMd5());
    }

    @ApiOperation("大文件分片上传前检验")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@Validated FileUploadQry fileUploadQry){

        return mediaFilesApplication.checkChunk(fileUploadQry.getFileMd5(), fileUploadQry.getChunk());
    }

    @ApiOperation("大文件分片上传")
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Void> uploadChunk(@RequestPart("file") MultipartFile file,
                                          @RequestParam("fileMd5") String fileMd5,
                                          @RequestParam("chunk") int chunk){

        return mediaFilesApplication.uploadChunk(file, fileMd5, chunk);
    }

    @ApiOperation("大文件分片合并")
    @PostMapping("/upload/mergechunks")
    public RestResponse<Void> mergeChunks(@RequestParam("fileMd5") String fileMd5,
                            @RequestParam("chunkTotal") Integer chunkTotal,
                            @RequestParam("fileName") String filName){
        Long companyId = 1232141425L; //todo

        return mediaFilesApplication.mergeChunks(fileMd5, chunkTotal, filName, companyId);
    }


    @ApiOperation("媒资列表")
    @PostMapping("/files")
    public PageResult<MediaFilesDto> mediaFilesPage(PageParams pageParams,
                                                    @RequestBody(required = false) MediaFilesPageQry mediaFilesPageQry){

        return mediaFilesApplication.mediaFilesPage(pageParams, mediaFilesPageQry);
    }

}
